package com.yellowpage.nsp.listener;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.junit.runner.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

















import com.yellowpage.client.data.NspUserAccount;
import com.yellowpage.client.data.NspUserPayAccount;
import com.yellowpage.client.data.NspUserPerson;
import com.yellowpage.client.service.NspUserAccountService;
import com.yellowpage.client.service.NspUserPayAccountService;
import com.yellowpage.client.service.NspUserPersonService;
import com.yellowpage.nsp.dao.RemoteNspUserDAO;
import com.yellowpage.nsp.data.NspFetchLog;
import com.yellowpage.nsp.service.NspFetchLogService;

public class FetchNSPUserTask extends TimerTask{
	
    ServletContextEvent _e;
    public FetchNSPUserTask(ServletContextEvent e) {
        _e = e;
    }

	@Override
	public void run() {
		/*同步user_account表*/

		NspFetchLogService nspFetchLogService;
		NspUserAccountService nspUserAccountService;
		NspUserPayAccountService nspUserPayAccountService;
		NspUserPersonService nspUserPersonService;
		WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(_e.getServletContext());
		nspFetchLogService = (NspFetchLogService) context.getBean("nspFetchLogService");
		nspUserAccountService= (NspUserAccountService) context.getBean("nspUserAccountService");
		nspUserPayAccountService= (NspUserPayAccountService) context.getBean("nspUserPayAccountService");
		nspUserPersonService= (NspUserPersonService) context.getBean("nspUserPersonService");
		
		List<Map> fetchlogerList=nspFetchLogService.getNSPSYSUpdateTime("user_account");
		String updateTime="0";
		if(fetchlogerList!=null && fetchlogerList.size()>0){
			if(fetchlogerList.get(0).get("END_TIME")!=null){
				updateTime=fetchlogerList.get(0).get("END_TIME").toString();
			}
		}
		
		
		List<Map> list=nspFetchLogService.getNSPUserAccount(updateTime);
		if(list.size()>0){
			for (Map map : list) {
				 NspUserAccount UserAccount = nspUserAccountService.getNspUserAccount(Long.parseLong(map.get("id").toString()));
				if(UserAccount!=null){
			        UserAccount.setId(Long.parseLong(map.get("id")!=null?map.get("id").toString():null));
			        UserAccount.setMobileNo(map.get("mobileNo")!=null?map.get("mobileNo").toString():null);
			        UserAccount.setPassword(map.get("password")!=null?map.get("password").toString():null);
			        UserAccount.setRegistTime(map.get("registTime")!=null?Timestamp.valueOf(map.get("registTime").toString()):null);
			        UserAccount.setAccountType(map.get("accountType")!=null?map.get("accountType").toString():null);
			        UserAccount.setAccountUser(map.get("accountUser")!=null?map.get("accountUser").toString():null);
			        UserAccount.setSubmitter(map.get("submitter")!=null?map.get("submitter").toString():null);
			        UserAccount.setSubmitTime(map.get("submitTime")!=null?Timestamp.valueOf(map.get("submitTime").toString()):null);
			        UserAccount.setUpdateUser(map.get("updateUser")!=null?map.get("updateUser").toString():null);
			        UserAccount.setUpdateTime(map.get("updateTime")!=null?Timestamp.valueOf(map.get("updateTime").toString()):null);
			        UserAccount.setDescription(map.get("description")!=null?map.get("description").toString():null);
			        nspUserAccountService.updateNspUserAccount(UserAccount);
					
				}else{
					NspUserAccount nspUserAccount = new NspUserAccount();
			 	   	nspUserAccount.setId(Long.parseLong(map.get("id")!=null?map.get("id").toString():null));
			 	   	nspUserAccount.setMobileNo(map.get("mobileNo")!=null?map.get("mobileNo").toString():null);
			 	   	nspUserAccount.setPassword(map.get("password")!=null?map.get("password").toString():null);
			 	   	nspUserAccount.setRegistTime(map.get("registTime")!=null?Timestamp.valueOf(map.get("registTime").toString()):null);
			 	   	nspUserAccount.setAccountType(map.get("accountType")!=null?map.get("accountType").toString():null);
			 	   	nspUserAccount.setAccountUser(map.get("accountUser")!=null?map.get("accountUser").toString():null);
			 	   	nspUserAccount.setSubmitter(map.get("submitter")!=null?map.get("submitter").toString():null);
			 	   	nspUserAccount.setSubmitTime(map.get("submitTime")!=null?Timestamp.valueOf(map.get("submitTime").toString()):null);
			 	   	nspUserAccount.setUpdateUser(map.get("updateUser")!=null?map.get("updateUser").toString():null);
			 	   	nspUserAccount.setUpdateTime(map.get("updateTime")!=null?Timestamp.valueOf(map.get("updateTime").toString()):null);
			 	   	nspUserAccount.setDescription(map.get("description")!=null?map.get("description").toString():null);
			 	   	nspUserAccountService.saveNspUserAccount(nspUserAccount);
				}
				
			}
			//修改更新时间
			if(fetchlogerList!=null && fetchlogerList.size()>0){
				Long longid=Long.parseLong(fetchlogerList.get(0).get("FETCH_LOG_ID").toString());
				NspFetchLog nspFetchLog=nspFetchLogService.getNspFetchLog(longid);
				if(nspFetchLog!=null){
					nspFetchLog.setEndTime(new Date());
					nspFetchLogService.updateNspFetchLog(nspFetchLog);
				}
			}else{
				NspFetchLog nspFetch = new NspFetchLog();
				nspFetch.setFetchTable("user_account");
				nspFetch.setServerId(1);
				nspFetch.setEndTime(new Date());
				nspFetchLogService.saveNspFetchLog(nspFetch);
			}
			
			
		}
			
		/*同步 user_pay_account*/
		List<Map> fetchlogerListPay=nspFetchLogService.getNSPSYSUpdateTime("user_pay_account");
		String payUpdateTime="0";
		if(fetchlogerListPay!=null && fetchlogerListPay.size()>0){
			if(fetchlogerListPay.get(0).get("END_TIME")!=null){
				payUpdateTime=fetchlogerListPay.get(0).get("END_TIME").toString();
			}
		}
		
		
		List<Map> listPay=nspFetchLogService.getNSPUserPayAccount(payUpdateTime);
		if(listPay.size()>0){
			for (Map map : listPay) {
				NspUserPayAccount nspUserPayAccount=nspUserPayAccountService.getNspUserPayAccount(Long.parseLong(map.get("id").toString()));
				
				if(nspUserPayAccount!=null){
					nspUserPayAccount.setId(Long.parseLong(map.get("id").toString()));
				
					nspUserPayAccount.setMobileNo(map.get("mobileNo")!=null?map.get("mobileNo").toString():null);
					nspUserPayAccount.setDiscountSource(map.get("discountSource")!=null?map.get("discountSource").toString():null);
					nspUserPayAccount.setDicountDesc(map.get("dicountDesc")!=null?map.get("dicountDesc").toString():null);
					nspUserPayAccount.setEffectTime(map.get("effectTime")!=null?Timestamp.valueOf(map.get("effectTime").toString()):null);
					nspUserPayAccount.setEffectState(map.get("effectState")!=null?map.get("effectState").toString():null);
					nspUserPayAccount.setPaySource(map.get("paySource")!=null?map.get("paySource").toString():null);
					nspUserPayAccount.setPay(map.get("pay")!=null?map.get("pay").toString():null);
					nspUserPayAccount.setCreateTime(map.get("createTime")!=null?Timestamp.valueOf(map.get("createTime").toString()):null);
					nspUserPayAccount.setPayState(map.get("payState")!=null?map.get("payState").toString():null);
					nspUserPayAccount.setSubmitter(map.get("submitter")!=null?map.get("submitter").toString():null);
					nspUserPayAccount.setSubmitTime(map.get("submitTime")!=null?Timestamp.valueOf(map.get("submitTime").toString()):null);
					nspUserPayAccount.setUpdateUser(map.get("updateUser")!=null?map.get("updateUser").toString():null);
					nspUserPayAccount.setUpdateTime(map.get("updateTime")!=null?Timestamp.valueOf(map.get("updateTime").toString()):null);
					nspUserPayAccount.setDescription(map.get("description")!=null?map.get("description").toString():null);
					
					nspUserPayAccountService.updateNspUserPayAccount(nspUserPayAccount);
				}else{
					NspUserPayAccount payaccount=new NspUserPayAccount();
					payaccount.setId(Long.parseLong(map.get("id").toString()));
					
					payaccount.setMobileNo(map.get("mobileNo")!=null?map.get("mobileNo").toString():null);
					payaccount.setDiscountSource(map.get("discountSource")!=null?map.get("discountSource").toString():null);
					payaccount.setDicountDesc(map.get("dicountDesc")!=null?map.get("dicountDesc").toString():null);
					payaccount.setEffectTime(map.get("effectTime")!=null?Timestamp.valueOf(map.get("effectTime").toString()):null);
					payaccount.setEffectState(map.get("effectState")!=null?map.get("effectState").toString():null);
					payaccount.setPaySource(map.get("paySource")!=null?map.get("paySource").toString():null);
					payaccount.setPay(map.get("pay")!=null?map.get("pay").toString():null);
					payaccount.setCreateTime(map.get("createTime")!=null?Timestamp.valueOf(map.get("createTime").toString()):null);
					payaccount.setPayState(map.get("payState")!=null?map.get("payState").toString():null);
					payaccount.setSubmitter(map.get("submitter")!=null?map.get("submitter").toString():null);
					payaccount.setSubmitTime(map.get("submitTime")!=null?Timestamp.valueOf(map.get("submitTime").toString()):null);
					payaccount.setUpdateUser(map.get("updateUser")!=null?map.get("updateUser").toString():null);
					payaccount.setUpdateTime(map.get("updateTime")!=null?Timestamp.valueOf(map.get("updateTime").toString()):null);
					payaccount.setDescription(map.get("description")!=null?map.get("description").toString():null);
					nspUserPayAccountService.saveNspUserPayAccount(payaccount);
				}
				
			}
			//修改更新时间
			if(fetchlogerListPay!=null && fetchlogerListPay.size()>0){
				Long longid=Long.parseLong(fetchlogerListPay.get(0).get("FETCH_LOG_ID").toString());
				NspFetchLog nspFetchLog=nspFetchLogService.getNspFetchLog(longid);
				if(nspFetchLog!=null){
					nspFetchLog.setEndTime(new Date());
					nspFetchLogService.updateNspFetchLog(nspFetchLog);
				}
			}else{
				NspFetchLog nspFetch = new NspFetchLog();
				nspFetch.setFetchTable("user_pay_account");
				nspFetch.setServerId(1);
				nspFetch.setEndTime(new Date());
				nspFetchLogService.saveNspFetchLog(nspFetch);
			}
		}
		
		
		
		/*同步 user_person*/
		List<Map> fetchlogerListPerson=nspFetchLogService.getNSPSYSUpdateTime("user_person");
		String personUpdateTime="0";
		if(fetchlogerListPerson!=null && fetchlogerListPerson.size()>0){
			if(fetchlogerListPerson.get(0).get("END_TIME")!=null){
				personUpdateTime=fetchlogerListPerson.get(0).get("END_TIME").toString();
			}
		}
		List<Map> listPerson=nspFetchLogService.getNspUserPerson(personUpdateTime);
		if(listPerson.size()>0){
			for (Map map : listPerson) {
				NspUserPerson nspUserPerson=nspUserPersonService.getNspUserPerson(Long.parseLong(map.get("id").toString()));
				if(nspUserPerson!=null){
					nspUserPerson.setId(Long.parseLong(map.get("id").toString()));
					nspUserPerson.setMobileNo(map.get("mobileNo")!=null?map.get("mobileNo").toString():null);
					nspUserPerson.setComAddr(map.get("comAddr")!=null?map.get("comAddr").toString():null);
					nspUserPerson.setComUser(map.get("comUser")!=null?map.get("comUser").toString():null);
					nspUserPerson.setComtel(map.get("comtel")!=null?map.get("comtel").toString():null);
					nspUserPerson.setZipcode(map.get("zipcode")!=null?map.get("zipcode").toString():null);
					nspUserPerson.setMerchatId(map.get("merchatId")!=null?map.get("merchatId").toString():null);
					nspUserPerson.setAttentionStock(map.get("attentionStock")!=null?map.get("attentionStock").toString():null);
					nspUserPerson.setAttentionTime(map.get("attentionTime")!=null?Timestamp.valueOf(map.get("attentionTime").toString()):null);
					nspUserPerson.setCancelTime(map.get("cancelTime")!=null?Timestamp.valueOf(map.get("cancelTime").toString()):null);
					nspUserPerson.setAttentionPrice(map.get("attentionPrice")!=null?map.get("attentionPrice").toString():null);
					nspUserPerson.setClassSequence(map.get("classSequence")!=null?map.get("classSequence").toString():null);
					nspUserPerson.setSubmitter(map.get("submitter")!=null?map.get("submitter").toString():null);
					nspUserPerson.setSubmitTime(map.get("submitTime")!=null?Timestamp.valueOf(map.get("submitTime").toString()):null);
					nspUserPerson.setUpdateTime(map.get("updateTime")!=null?Timestamp.valueOf(map.get("updateTime").toString()):null);
					nspUserPerson.setUpdateUser(map.get("updateUser")!=null?map.get("updateUser").toString():null);
					nspUserPerson.setDescription(map.get("description")!=null?map.get("description").toString():null);
					nspUserPersonService.updateNspUserPerson(nspUserPerson);
				}else{
					NspUserPerson Person=new NspUserPerson();
					
					Person.setId(Long.parseLong(map.get("id").toString()));
					Person.setMobileNo(map.get("mobileNo")!=null?map.get("mobileNo").toString():null);
					Person.setComAddr(map.get("comAddr")!=null?map.get("comAddr").toString():null);
					Person.setComUser(map.get("comUser")!=null?map.get("comUser").toString():null);
					Person.setComtel(map.get("comtel")!=null?map.get("comtel").toString():null);
					Person.setZipcode(map.get("zipcode")!=null?map.get("zipcode").toString():null);
					Person.setMerchatId(map.get("merchatId")!=null?map.get("merchatId").toString():null);
					Person.setAttentionStock(map.get("attentionStock")!=null?map.get("attentionStock").toString():null);
					Person.setAttentionTime(map.get("attentionTime")!=null?Timestamp.valueOf(map.get("attentionTime").toString()):null);
					Person.setCancelTime(map.get("cancelTime")!=null?Timestamp.valueOf(map.get("cancelTime").toString()):null);
					Person.setAttentionPrice(map.get("attentionPrice")!=null?map.get("attentionPrice").toString():null);
					Person.setClassSequence(map.get("classSequence")!=null?map.get("classSequence").toString():null);
					Person.setSubmitter(map.get("submitter")!=null?map.get("submitter").toString():null);
					Person.setSubmitTime(map.get("submitTime")!=null?Timestamp.valueOf(map.get("submitTime").toString()):null);
					Person.setUpdateTime(map.get("updateTime")!=null?Timestamp.valueOf(map.get("updateTime").toString()):null);
					Person.setUpdateUser(map.get("updateUser")!=null?map.get("updateUser").toString():null);
					Person.setDescription(map.get("description")!=null?map.get("description").toString():null);
					nspUserPersonService.saveNspUserPerson(Person);
					
				}				
			}
			//修改更新时间
			if(fetchlogerListPerson!=null && fetchlogerListPerson.size()>0){
				NspFetchLog nspFetchLog=nspFetchLogService.getNspFetchLog(Long.parseLong(fetchlogerListPerson.get(0).get("FETCH_LOG_ID").toString()));
				if(nspFetchLog!=null){
					nspFetchLog.setEndTime(new Date());
					nspFetchLogService.updateNspFetchLog(nspFetchLog);
				}
			
			}else{
				NspFetchLog nspFetch = new NspFetchLog();
				nspFetch.setFetchTable("user_pay_account");
				nspFetch.setServerId(1);
				nspFetch.setEndTime(new Date());
				nspFetchLogService.saveNspFetchLog(nspFetch);
			}
			
			
		}
		
		
		
		// TODO Auto-generated method stub
		
	}
	
	

	



}
